package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyBean;
import com.project.mapper.IBuyMapper;
import com.project.service.IBuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class BuyService implements IBuyService {
    @Autowired
    private IBuyMapper iBuyMapper;


    @Override
    public String findByGoodsId(String goodsId) {
        BuyBean buyBean = iBuyMapper.selectById(goodsId);
        if (buyBean == null) {
            return "add";
        }else {
            return "update";
        }
    }

    @Override
    public BuyBean findByGoodsId(Integer userId, String goodsId) {
        QueryWrapper<BuyBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        qw.eq("fk_goodsId",goodsId);

        return iBuyMapper.selectOne(qw);
    }

    @Override
    public void add(String goodsId, Integer num, Integer price ,Integer userId) {
            iBuyMapper.insert(new BuyBean(price,num,goodsId,userId));
    }

    @Override
    public void deleteById(Integer buyId) {
        iBuyMapper.deleteById(buyId);
    }

    @Override
    public void deleteByUserId(Integer userId) {
        QueryWrapper<BuyBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        iBuyMapper.delete(qw);
    }

    @Override
    public List<BuyBean> findAll(Integer userId) {
       return iBuyMapper.findAll(userId);
    }

    /**
     * 修改采购车信息
     * @param buyId 采购车id
     * @param newPrice 新采购价
     * @param newNum 新采购数量
     */
    @Override
    public void updateById(Integer buyId, Integer newPrice, Integer newNum) {
        BuyBean buyBean = iBuyMapper.selectById(buyId);
        buyBean.setPrice(newPrice);
        buyBean.setNum(newNum);
        iBuyMapper.updateById(buyBean);
    }

    @Override
    public void updateBuyNum(Integer buyId, Integer newBuyNum) {
        BuyBean buyBean = iBuyMapper.selectById(buyId);
        buyBean.setNum(newBuyNum);
        iBuyMapper.updateById(buyBean);
    }
    @Override
    public void updateBuyPrice(Integer buyId, Integer newBuyPrice) {
        BuyBean buyCar = iBuyMapper.selectById(buyId);
        buyCar.setPrice(newBuyPrice);
        iBuyMapper.updateById(buyCar);
    }
}
